package com.huluobo.high3videoproject.mvp.search.presenter;

import com.huluobo.base.view.BasePresenter;
import com.huluobo.high3videoproject.bean.VideoBeanResult;
import com.huluobo.high3videoproject.mvp.search.model.SearchModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SearchPresenter extends BasePresenter<SearchContract.View> implements SearchContract.Presenter {

    private SearchModel searchModel = new SearchModel();

    @Override
    public void getVideos(int currentPage, int size) {
        searchModel.getVideos(currentPage, size).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(VideoBeanResult videoBeanResult) {
                        if (isAttach()) {
                            getView().onGetVideosSuccess(videoBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onGetVideosFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void findVideoByName(String keywords) {
        searchModel.findVideoByName(keywords).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(VideoBeanResult videoBeanResult) {
                        if (isAttach()) {
                            getView().onFindVideoSuccess(videoBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onFindVideoFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
